package us.nchu.tomtiger.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import us.nchu.tomtiger.dao.ProjectDao;
import us.nchu.tomtiger.dao.ProjectPendingDao;
import us.nchu.tomtiger.dao.UserDao;
import us.nchu.tomtiger.model.Collection;
import us.nchu.tomtiger.model.Project;
import us.nchu.tomtiger.model.ProjectPending;
import us.nchu.tomtiger.model.User;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Service
public class ProjectService {
    @Resource
    private ProjectDao projectDao;
    @Resource
    private ProjectPendingDao projectPendingDao;

    public void createUncheckedProject(Project project) {
        projectDao.insert(project);
    }

    public List<Project> queryAllProject() {
        return projectDao.queryAll();
    }

    public Project getProjectByOwner(String phoneNumber) {
        return projectDao.queryByOwner(phoneNumber);
    }

    public Project getProjectById(String id) {
        return projectDao.queryById(id);
    }

    public List<Project> getProjectListByCollectionList(List<Collection> collections) {
        if (collections.size() == 0) {
            return new ArrayList<Project>();
        }

        StringBuilder stringBuilder = new StringBuilder(" id=").append(collections.get(0).getProjectId());
        for (int i = 1; i < collections.size(); i++) {
            stringBuilder.append(" or id=").append(collections.get(i).getProjectId());
        }

        return projectDao.getListForSqlString(stringBuilder.toString(), 1);
    }

    public List<Project> getProjectListByPendingList(List<ProjectPending> projectPendingList) {
        if (projectPendingList.size() == 0) {
            return new ArrayList<Project>();
        }

        StringBuilder stringBuilder = new StringBuilder(" id=").append(projectPendingList.get(0).getPendingProjectId());
        for (int i = 1; i < projectPendingList.size(); i++) {
            stringBuilder.append(" or id=").append(projectPendingList.get(i).getPendingProjectId());
        }

        return projectDao.getListForSqlString(stringBuilder.toString(), 0);
    }

    public Project getProjectById(int id) {
        return projectDao.queryById(id);
    }

    public void updateProjectInfo(Project project) {
        projectDao.update(project);
    }

    public void deleteProjectById(String id) {
        projectDao.deleteById(id);
    }

    public void deleteProjectById(int id) {
        projectDao.deleteById(id);
    }
}
